/**
 * 机构树jQuery插件
 * @author 329202 RickyFu
 */
(function($){
	
	var content = [		
		'<div class="modal">',
			'<div><label>机构代码：</label><span><input type="text" class="all_input" id="orgCode" name="orgCode"/></span><label>机构名称：</label><span><input type="text" class="all_input" id="orgName" name="orgName"/></span></div>',
			'<div style="margin:10px 0; padding-left:90px;"><input type="button" id="query" class="btn_blue m-r15" value="查 询"/></div>',
			'<div class="clearfix table_fn"><span>机构列表</span></div>',
			'<table id="grid"></table>',
			'<div id="pager"></div>',
		'</div>'
	].join('');
	
	var gridInit = function($grid, dia, $el, callback){
		$grid.jqGrid({
			datatype: "local",
			mtype: 'post',
			colNames: ['节点编号', '机构代码', '机构名称', '机构等级', '机构类型'],
			colModel: [
				{name: 'id', index: 'id', hidden: true},
				{name: 'orgCode', index: 'orgCode', width: 50},
				{name: 'orgName', index: 'orgName'},
				{name: 'orgLevel', index: 'orgLevel', hidden: true},
				{name: 'typeCode', index: 'typeCode', hidden: true}
			],
			height: 235,
			autowidth: true,
			viewrecords: true,
			multiselect: false,
			rownumbers: true,
			rowNum: 10,
			rowList: [10, 20, 30],
			pager: "#pager",
			jsonReader: {root: "data", id: "id", page: "index", records: "totalRecord", repeatitems: false},
			ondblClickRow: function(id, row, col, e){//onSelectRow: function(id, status)
				if(callback) {
					callback.call($el, $grid.jqGrid('getRowData', id));
				}
				dia.close();
			}
		});
	};
	
	var showOrg = function($el, callback){
		var dia = art.dialog({id: "treeTemp", title: '选择机构', width: '600px'});
		dia.lock();
		dia.content(content);// 填充对话框内容
		var $grid = $("#grid"), $orgCode = $("#orgCode"), $orgName = $("#orgName"), $modal = $('.modal');
		$modal.find('label').css({'width': '90px', 'display': 'inline-block', 'text-align': 'right'});
		$('#query').live('click', function(e){
			$.extend($grid.jqGrid("getGridParam", "postData"), {
				orgCode: $orgCode.val(), orgName: $orgName.val(), rand: Math.random()
			});
			$grid.jqGrid("setGridParam", {url: 'organization/view', datatype : 'json'}).trigger("reloadGrid", [{page: 1}]);
		});
		gridInit($grid, dia, $el, callback);
	};
	
	//选择机构
	$.fn.selOrg = function(callback){
		var $el	= $(this);
		$el.bind('click', function(e){
			e.preventDefault();
			showOrg($el, callback);
		});
	};
	
})(jQuery);


/**
 * 树型机构树
 * @author 329202 RickyFu
 */
(function($){
	
	var setting = {
			async: {enable: true, autoParam: ["id"], url: "organ/tree"},
			data: {
				key: {name: 'codeName'},
				simpleData: {enable: true, idKey: "id", pIdKey: "parentId", rootPId: 0}
			}
		},
		$text = $('<div class="skins_tree"></div>').bind('click', function(e){
			$main.toggle();
		}),
		$main = $('<div class="ztree_public" style="display: none;"></div>').bgiframe(),
		$bd = $('<ul class="ztree" style="overflow: auto;"></ul>'),
		zTree = null;
	
	/**
	 * 调用Api
	 * 若有2个参数,则第一个参数格式: {width: 300, height: 240}
	 * 最后一个参数: fn
	 */	
	$.fn.orgtree = function(){
		var args = arguments, len = args.length,
			op = (len == 2 ? args[0] : null)||{}, w = op.width||300, h = op.height||240,
			callback = len >= 1 ? args[len-1] : null;
			$node = $(this).addClass('dtree_p').append($('<span class="ztree_pic"></span>').hide());
			
		$node.append($text).append($main.css({width: w}).append($bd.css({height: h}))).parent().addClass('dtree_frame');
		
		zTree = $.fn.zTree.init($bd, $.extend(setting, {callback: {onClick: function(e, treeId, treeNode){
			var selNode = zTree.getSelectedNodes()[0];
			$main.hide();
			$text.text(selNode.orgName);
			callback ? callback.call($node, selNode) : '';
		}}}));
	};
	
	//清除选择框文本
	$.fn.clearTreeText = function(){
		$text.text('');//未清除zTree选中状态...
	};
	
})(jQuery);